<template>
  <div >
    <div class="table_div">
      <!-- 搜索 -->
      <el-form ref="formSearch" :model="params" :inline="true" size="small">
        <el-form-item label="任务名称" prop="name">
          <el-input
            v-model="params.name"
            clearable
            class="filter-item"
            placeholder="支持模糊查询"
          />
        </el-form-item>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toPageQuery">搜索</el-button>
        <el-button class="filter-item" size="mini" icon="el-icon-close" @click="resetSearchData">重 置</el-button>
      </el-form>
    </div>
    <div class="table_div">
      <el-button type="primary" size="mini" icon="el-icon-plus" @click="add">新 增</el-button>
      <!-- 任务日志 -->
      <div style="display: inline-block;">
        <el-button
          class="filter-item"
          size="mini"
          type="info"
          icon="el-icon-tickets"
          @click="doLog">日志</el-button>
        <Log ref="log"/>
      </div>
    </div>
    <!--表单组件-->
    <eForm ref="form" :is-add="isAdd"/>
    <!--表格渲染-->
    <el-table
      v-loading="loading"
      :data="data"
      border
      stripe
      highlight-current-row
      size="small"
      style="width: 100%;"
      >
      <el-table-column prop="className" label="任务类路径" align="center"/>
      <el-table-column prop="cronExpression" label="cron表达式" align="center"/>
      <el-table-column prop="state" label="状态" width="90px" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.state =='1'? 'warning' : 'success'">{{ scope.row.state =='1'? '已暂停' : '运行中' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="prevExecuteTime" label="上次执行时间" align="center"/>
      <el-table-column prop="nextExecuteTime" label="下次执行时间" align="center"/>
      <el-table-column :show-overflow-tooltip="true" prop="params" label="描述" align="center"/>
      <el-table-column label="操作" width="180px" align="center" fixed="right" >
        <template slot-scope="scope">
          <el-button style="margin-left: -2px" type="text" size="mini" @click="execute(scope.row.id)">执行一次</el-button>
          <el-button style="margin-left: 3px" type="text" size="mini" @click="updateStatus(scope.row.id,scope.row.state=='1' ? '恢复' : '暂停')">
            {{ scope.row.state=='1' ? '恢复' : '暂停' }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px" align="center" fixed="right" >
        <template slot-scope="scope">
          <el-button  size="mini" style="margin-right: 3px;" type="text" @click="edit(scope.row)">编辑</el-button>
          <el-popover
            :ref="scope.row.id"
            placement="top"
            width="200">
            <p>确定停止并删除该任务吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="text" size="mini">删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
/* eslint-disable */
import Config from '@/config'
import initData from '@/mixins/initData'
import { del, execution, pause, resume } from '@/api/timing'
import Log from './log'
import eForm from './form'
export default {
  name: 'Timing',
  components: { Log, eForm },
  mixins: [initData],
  data() {
    return {
      delLoading: false,
      loading: false,
      dialog: false
    }
  },
  created() {
    this.$nextTick(() => {
      this.initPageData()
    })
  },
  methods: {
    beforeInit() {
      this.url = 'timingJob/listPage'
      return true
    },
    add() {
      this.isAdd = true
      const _this = this.$refs.form
      _this.form = {
        state: '0'
      }
      _this.dialog = true
    },
    execute(id) {
      execution(id).then(res => {
        if (res.code == Config.code_200) {
          this.$notify({title: '执行成功', type: 'success',})
        } else {
          this.$notify.error({title: '失败', message: res.msg})
        }
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    updateStatus(id, status) {
      if ('恢复'==status) {
        resume(id).then(res => {
          if (res.code == Config.code_200) {
            this.$notify({title: status + '成功', type: 'success',})
          } else {
            this.$notify.error({title: status +  '失败', message: res.msg})
          }
        }).catch(err => {
          console.log(err.response.data.message)
        })
      }else {
        pause(id).then(res => {
          if (res.code == Config.code_200) {
            this.$notify({title: status + '成功', type: 'success',})
          } else {
            this.$notify.error({title: status +  '失败', message: res.msg})
          }
        }).catch(err => {
          console.log(err.response.data.message)
        })
      }
      this.initPageData()
    },
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        if (res.code == Config.code_200) {
          this.initPageData()
          this.dleChangePage()
          this.$notify({title: '删除成功', type: 'success',})
        } else {
          this.$notify.error({title: '失败', message: res.msg})
        }
        this.delLoading = false
        this.$refs[id].doClose()

      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    },
    doLog() {
      this.$refs.log.dialog = true
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.form = data
      _this.dialog = true
    }
  }
}
</script>
<style scoped>
.table_div {
  padding: 8px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  -webkit-transition: 0.3s;
  transition: 0.3s;
}

</style >
